<?php
require_once APP . '/application/controllers/UserAction.php';
require_once(APP . '/application/models/MeetModel.php');
require_once(APP . '/application/models/Article.php');
require_once(APP . '/application/models/UserModel.php');
require_once(APP . '/application/models/InvitecodeModel.php');

/**
 * 邀请码
 * @package admin
 */
class InviteController extends UserAction
{
    

    public function init()
    {
        $mid = (int)$this->getParam("mid");
    	$uid = (int)$_SESSION['uid'];
    	
    	if ($mid != 0 && $uid != 0){
    		$exists = $this->db->fetchOne("select count(*) from meet where mid=? and uid=?", array($mid, $uid));
    		if ($exists == false){
    			exit("该会议不是你创建的！");
    		}
    	}
    }
    
    
    public function importAction()
    {
    	$mid = (int)$this->getParam("mid");
    	
    	
    	$this->view->mid = $mid;
    	$this->render('import');
    }
    
    public function doimportAction()
    {
    	set_time_limit(0);
    	ini_set('memory_limit', '-1');
    	
    	$mid = (int)$this->getParam("mid");
    	
    	$url = $this->url($this->baseUrl, "invite", "import", "mid=" . $mid);
    	
    	if (empty($_FILES['file']) || empty($_FILES['file']['tmp_name']))
    		$this->showtipmessage($url, "请选择上传文件");
    	
    	$excel_file = $_FILES['file']['tmp_name'];
    	
    	// 导入excel
    	include APP . '/application/models/PHPExcel/IOFactory.php';
    	
    	$objReader = new PHPExcel_Reader_Excel2007();
    	$objPHPExcel = $objReader->load($excel_file);
    	
    	$sheet = $objPHPExcel->getSheetByName("mail");
    	if ($sheet == null){
    		$this->showtipmessage($url, "工作表：mail未找到！");
    	}
    	
    	$arr_data = array();
    	$rows = $sheet->getHighestDataRow();
    	for($row = 2; $row <= $rows; $row++){
    		$col1 = $sheet->getCellByColumnAndRow(0, $row)->getCalculatedValue();
    		$col2 = $sheet->getCellByColumnAndRow(1, $row)->getCalculatedValue();
    		$col3 = $sheet->getCellByColumnAndRow(2, $row)->getCalculatedValue();
    		$col4 = $sheet->getCellByColumnAndRow(3, $row)->getCalculatedValue();
    		$col5 = $sheet->getCellByColumnAndRow(4, $row)->getCalculatedValue();
    		
			$col1 = trim($col1);
			if (empty($col1)) continue;			
			
    		// 如果格式为邮件，则开始导入
    		if (ereg("^[A-Za-z0-9\.|_|-]*[@]{1}[A-Za-z0-9\.|_|-]*[.]{1}[a-z]{2,5}$",$col1)){
    			$arr_data[] = array("email" => $col1, "name" => $col2, "ncall" => $col3, "enname" => $col4, "encall" => $col5);
    		}else{
				$this->showtipmessage($url, '第'.$row.'行 - '.$col1.'这个邮件地址不正确，请核对，导入终止！');
				exit;
			}
    		
    		/*
			if (substr_count ($col1, "@") > 1)
    		{
    			$this->showtipmessage($url, "邮件格式错误，请检查!");
    		}  */
    	}
    	
    	unlink($excel_file);
    	if (count($arr_data) == 0){
    		$this->showtipmessage($url, "未找到导入数据!");
    	}
    	
    	// 去重处理
    	$arr_db = $this->db->fetchAll("select email from invitecode where mid=?", array($mid));
    	$arr_mail_keys = array();
    	foreach($arr_db as $v){
    		$arr_mail_keys[$v['email']] = true;
    	}
    	
    	foreach($arr_data as $index => $v){
    		if (isset($arr_mail_keys[$v['email']])){
    			unset($arr_data[$index]);
    		}else{
    			$arr_mail_keys[$v['email']] = true;
    		}
    	}
    	
    	// 生成邀请码
    	foreach($arr_data as $index => $v){
    		$str = md5($mid . $v['email'] . $v['call'] . $v['name'] . $this->cfg['slot']);
    		$arr_data[$index]['code'] = strtoupper(substr($str, 10, 10));
    	}
    	
    	// 插入数据库
    	$now = date("Y-m-d H:i:s", time());
    	if (count($arr_data) > 0){
    		$sql = "insert into invitecode(mid, email, name, ncall, enname, encall, code, createtime) values";
    		$comma = "";
    		foreach($arr_data as $v){
    			foreach($v as $index => $vv){
    				$v[$index] = mysql_escape_string(trim($vv));
    			}
    			$sql .= $comma;
    			$comma = ",";
    			$sql .= sprintf('(%d, "%s", "%s", "%s", "%s", "%s" ,"%s", "%s")', $mid, $v['email'], $v['name'], $v['ncall'],$v['enname'], $v['encall'], $v['code'], $now);
    		}
    		
    		$this->db->query($sql);
    	}
    	

    	$this->showtipmessage($url, "导入成功!共导入" . count($arr_data) . "条记录");
    }
    
    
    /**
     * 邀请码列表
     */
    public function listAction()
    {
    	$mid = (int)$this->getParam("mid");
    	$nowp = $this->getParam ('nowp', 1);
    	$use = $this->getParam("use");
    	$per_page = 30;
    	
    	$obj = new InvitecodeModel($this->db);
    	$result = $obj->fetchAll ($mid, $use, $nowp, $per_page);
    	
    	$this->view->list = $result['list'];
    	$this->view->page = $result['page'];
    	$this->view->mid = $mid;
    	$this->view->use = $use;
    	
    	$this->render("list");
    }
    
    public function delAction()
    {
    	$mid = (int)$this->getParam("mid");
    	$codeid = (int)$this->getParam("codeid");
    	$obj = new InvitecodeModel($this->db);
    	$obj->del($codeid, $mid);
    	echo json_encode(array("status" => 1));
    }
    
    public function searchAction()
    {
    	$mid = (int)$this->getParam("mid");
    	$email = $this->getParam("email");
    	
    	 
    	$obj = new InvitecodeModel($this->db);
    	$list = $obj->search($mid, $email);
    	 
    	$this->view->list = $list;
    	$this->view->mid = $mid;
    	$this->view->email = htmlspecialchars($email);
    	
    	$this->render('search');
    }
    
    /**
     * 修改
     */
    public function changeAction()
    {
    	$codeid = (int)$_REQUEST['codeid'];
    	$mid = (int)$_REQUEST['mid'];
    	
    	$obj = new InvitecodeModel($this->db);
    	
    	$button = $_REQUEST['button'];
    	$change = false;
    	if (!empty($button))
    	{
    		$value = array();
    		$value['name'] = trim ($this->getParam("name"));
    		$value['ncall'] = trim ($this->getParam("ncall"));
    		$value['enname'] = trim ($this->getParam("enname"));
    		$value['encall'] = trim ($this->getParam("encall"));
    		
    		$obj->change($codeid, $value);
    		$change =true;
    	}
    	
    	$this->view->info = $obj->show($codeid);
    	$this->view->mid = $mid;
    	$this->view->setflag = $change;
    	
    	$this->render("change");
    }
    
    public function sendAction()
    {
    	$mid = (int)$this->getParam("mid");
    	
    	$obj_meet = new MeetModel($this->db);
    	
    	$content = $obj_meet->show($mid);
    	$url = $this->view->fullUrl . '/' . $content['domain'];
    	$eurl = $this->url($this->baseUrl, "invite", "send", "mid=" . $mid);
    	
    	if ($this->getParam("button")!=null){
    		$stype = $this->getParam("stype");
    		$start = (int)$this->getParam("start");
    		$end = (int)$this->getParam("end");
    		$content = $this->getParam("content");
    		$title = $this->getParam("title");
    		
    		if ($stype == "all"){
    			$arr_list = $this->db->fetchAll("select * from invitecode where mid=?", array($mid));
    		}else{
    			$arr_list = $this->db->fetchAll("select * from invitecode where mid=? and codeid>=? and codeid<=?", array($mid, $start, $end));
    		}
    		
    		if (count($arr_list) == 0){
    			$this->showtipmessage($eurl, "发送列表为空！");
				exit;
			}
    		
    		if (empty($title)){
    			$this->showtipmessage($eurl, "标题不能为空！");
				exit;
    		}
    		if (empty($content)){
    			$this->showtipmessage($eurl, "正文不能为空！");
				exit;
    		}
    		
    		// 开始发送
    		$param = array();
    		$param['mid'] = $mid;
    		$param['stype'] = $stype;
    		$param['start'] = $start;
    		$param['end'] = $end;
    		$param['title'] = $title;
    		$param['content'] = $content;
    		$param['url'] = $url;
    		$param['nametype'] = $this->getParam("nametype");
    		$param['fontname'] = $this->getParam("fontname");
    		$param['fontsize'] = $this->getParam("fontsize");
    		$param['ccmail'] = $this->getParam("ccmail");
    		
    		Util::callbackcmd("invite.php", $param);
    		
    		$this->showtipmessage($eurl, "已加入发送队列，发送中...\\n共" . count($arr_list) . '个用户');
    		$this->view->content = $content;
    	}
    	
    	
    	$this->view->mid = $mid;
    	$this->view->url = $url;
    	$this->render("send");
    }
    
    /**
     * 发送最终确认
     */
    public function confirmAction()
    {
    	$mid = (int)$this->getParam("mid");

		$status = (int)$this->getParam("status");
    	$stype = $this->getParam("stype");
    	$uid_start = (int)$this->getParam("start");
    	$uid_end = (int)$this->getParam("end");
    	 
    	$obj_meet = new MeetModel($this->db);
    	 
    	$content = $obj_meet->show($mid);
    	$url = $this->view->fullUrl . '/' . $content['domain'];
    	$eurl = $this->url($this->baseUrl, "invite", "confirm", "mid=" . $mid);
    	 
    	if ($this->getParam("button")!=null){
    		$content = $this->getParam("content");
    		$title = $this->getParam("title");
    		
    		if ($status == 3){
    			$where = "";
    		}else{
    			$where = "and ischange=" . $status;
    		}
    		
    		if ($stype == 'range'){
    			$where .= ' and uid>=' . $uid_start . ' and uid<=' . $uid_end;
    		}
    
    		$arr_list = $this->db->fetchOne("select count(*) from meetuser where mid=? and ischeck=2 $where", array($mid));
    
    		if ($arr_list == 0){
    			$this->showtipmessage($eurl, "发送列表为空！");
				exit;
			}
    
    		if (empty($title)){
    			$this->showtipmessage($eurl, "标题不能为空！");
				exit;
    		}
    		if (empty($content)){
    			$this->showtipmessage($eurl, "正文不能为空！");
				exit;
    		}
    
    		// 开始发送
    		$param = array();
    		$param['mid'] = $mid;
    		$param['title'] = $title;
    		$param['content'] = $content;
    		$param['url'] = $url;
    		$param['nametype'] = $this->getParam("nametype");
    		$param['fontname'] = $this->getParam("fontname");
    		$param['fontsize'] = $this->getParam("fontsize");
    		$param['ccmail'] = $this->getParam("ccmail");
    		
    		$param['status'] = $status;
    		$param['stype'] = $stype;
    		$param['uid_start'] = $uid_start;
    		$param['uid_end'] = $uid_end;
    
    		Util::callbackcmd("confirm.php", $param);
    
    		$this->showtipmessage($eurl, "已加入发送队列，发送中...\\n共" . $arr_list . '个用户');
    		$this->view->content = $content;
    	}
    	 
    	 
    	$this->view->mid = $mid;
    	$this->view->url = $url;
    	$this->render("confirm");
    }
    
    
    /**
     * 邮件群发
     */
    public function allsendAction()
    {
    	$mid = (int)$this->getParam("mid");
    	
    	$status = (int)$this->getParam("status");
    	$stype = $this->getParam("stype");
    	$uid_start = (int)$this->getParam("start");
    	$uid_end = (int)$this->getParam("end");
    	$nation = $this->getParam("snation", "");
    
    	$obj_meet = new MeetModel($this->db);
    
    	$content = $obj_meet->show($mid);
    	$url = $this->view->fullUrl . '/' . $content['domain'];
    	$eurl = $this->url($this->baseUrl, "invite", "allsend", "mid=" . $mid);
    	
    	$arr_nations = $this->db->fetchAll("select distinct(user.nation) as nation from meetuser t1 left join user on user.uid=t1.uid  where t1.mid=? ", array($mid));
    
    	if ($this->getParam("button")!=null){
    		$content = $this->getParam("content");
    		$title = $this->getParam("title");
    
    		if ($status == 0){
    			$where = "";
    		}else{
    			$where = "and ischeck=" . $status;
    		}
    		
    		if ($stype == 'range'){
    			$where .= ' and uid>=' . $uid_start . ' and uid<=' . $uid_end;
    		}
    		
    		$arr_list = $this->db->fetchOne("select count(*) from meetuser where mid=? $where", array($mid));
    
    		if ($arr_list == 0){
    			$this->showtipmessage($eurl, "发送列表为空！");
				exit;
			}
			
			if ($nation != 'all'){
				$snation_t = $this->getParam("snation_t", 0);
				
				if ($status == 0){
					$where = "";
				}else{
					$where = " and t1.ischeck=" . $status;
				}
				
				if ($stype == 'range'){
					$where .= '  and t1.uid>=' . $uid_start . ' and t1.uid<=' . $uid_end;
				}
				
				
				if ( $snation_t == 0)
					$count = $this->db->fetchOne("select count(*) from meetuser t1 left join user on user.uid=t1.uid  where t1.mid=? and user.nation=\"?\"" . $where, array($mid, $nation));
				else
					$count = $this->db->fetchOne("select count(*) from meetuser t1 left join user on user.uid=t1.uid  where t1.mid=? and user.nation!=\"?\"" . $where, array($mid, $nation));
					
				$arr_list = $count;
				//	$count_arr = $this->db->fetchAll("select * from meetuser t1 left join user on user.uid=t1.uid  where t1.mid=? and user.nation=\"?\"", array($mid, $nation));
				
				if ($count == 0){
					$this->showtipmessage($eurl, "发送列表为空！");
					exit;
				}
			}
			
    		if (empty($title)){
    			$this->showtipmessage($eurl, "标题不能为空！");
				exit;
    		}
    		if (empty($content)){
    			$this->showtipmessage($eurl, "正文不能为空！");
				exit;
    		}
    
    		// 开始发送
    		$param = array();
    		$param['mid'] = $mid;
    		$param['title'] = $title;
    		$param['content'] = $content;
    		$param['url'] = $url;
    		$param['snation'] = $this->getParam("snation", "");
    		$param['snation_t'] = $this->getParam("snation_t", 0);

    		$param['nametype'] = $this->getParam("nametype");
    		$param['fontname'] = $this->getParam("fontname");
    		$param['fontsize'] = $this->getParam("fontsize");
    		$param['ccmail'] = $this->getParam("ccmail");
    		
    		$param['status'] = $status;
    		$param['stype'] = $stype;
    		$param['uid_start'] = $uid_start;
    		$param['uid_end'] = $uid_end;
    		    
    		Util::callbackcmd("allsend.php", $param);
    
    		$this->showtipmessage($eurl, "已加入发送队列，发送中...\\n共" . $arr_list . '个用户');
    		$this->view->content = $content;
    	}
    
    
    	$this->view->mid = $mid;
    	$this->view->url = $url;
    	$this->view->nations = $arr_nations;
    	$this->render("allsend");
    }
    
    

    /**
     * 邮件群发
     */
    public function nlsendAction()
    {
    	$mid = (int)$this->getParam("mid");
    
    	$obj_meet = new MeetModel($this->db);
    
    	$content = $obj_meet->show($mid);
    	$url = $this->view->fullUrl . '/' . $content['domain'];
    	$eurl = $this->url($this->baseUrl, "invite", "nlsend", "mid=" . $mid);
    
    	if ($this->getParam("button")!=null){
    		$content = $this->getParam("content");
    		$title = $this->getParam("title");
        
    		if (empty($title)){
    			$this->showtipmessage($eurl, "标题不能为空！");
				exit;
    		}
    		if (empty($content)){
    			$this->showtipmessage($eurl, "正文不能为空！");
				exit;
    		}
    		
    		$file = Util::upfile("file", "mail");
    
    		// 开始发送
    		$param = array();
    		$param['mid'] = $mid;
    		$param['title'] = $title;
    		$param['content'] = $content;
    		$param['file'] = $file;
			$param['url'] = $url;

    		$param['nametype'] = $this->getParam("nametype");
    		$param['fontname'] = $this->getParam("fontname");
    		$param['fontsize'] = $this->getParam("fontsize");
    		$param['ccmail'] = $this->getParam("ccmail");
    		
    		Util::callbackcmd("nlsend.php", $param);
    
    		$this->showtipmessage($eurl, "已加入发送队列，发送中...");
    		$this->view->content = $content;
    	}
    
    
    	$this->view->mid = $mid;
    	$this->view->url = $url;
    	$this->render("nlsend");
    }
}